/********************************************************************************
 * Copyright (c)2016-2020, ShenZhen WanJiaAn Technology CO.,LTD.
 * All Rights Reserved.
 *
 * File Name     : hi_hal_syslink.h
 * Author        : joki.zhu
 * Date          : 2020-07-28
 *
 * Record        :
 * 1.Date        : 2020-07-28
 *   Author      : joki.zhu
 *   Modification: Created file
 ********************************************************************************/

#ifndef __HI_HAL_SYSLINK_H__
#define __HI_HAL_SYSLINK_H__

#include "hi_type.h"
#include "hi_syslink.h"

#ifdef __cplusplus
#if __cplusplus
extern "C"{
#endif
#endif /* __cplusplus */

#define HAL_SYSLINK_COMPONENT           (0xA)
#define HAL_SYSLINK_MODULE              (0xB)
#define HAL_SYSLINK_MSG_LEN             (488)
#define HAL_SYSLINK_MSG_TIMEOUT_MS      (1000)
#define HAL_SYSLINK_MODULE_ID           hi_syslink_mod_id_define(HAL_SYSLINK_COMPONENT, HAL_SYSLINK_MODULE)

#define HAL_SYSLINK_UPGRADE_BLOCK_SIZE  (256)

typedef enum {
    HAL_SYSLINK_MSG_QOS_DIRECTLY = 0, /* msg will been sended directly without response */
    HAL_SYSLINK_MSG_QOS_WITH_RESP,    /* msg should been sended and responsed by remote */
    HAL_SYSLINK_MSG_QOS_BUTT
} HAL_SYSLINK_MsgQos;

typedef enum {
    HI_SYSLINK_MSGID_TEXT                       = hi_syslink_msg_id_define(HAL_SYSLINK_COMPONENT, HAL_SYSLINK_MODULE, 0x01),

    /* wifi to soc msg id  */
    HI_SYSLINK_MSGID_W2S_PIR_EVENT              = hi_syslink_msg_id_define(HAL_SYSLINK_COMPONENT, HAL_SYSLINK_MODULE, 0x02),

    ///< 1Byte按键类型(0:门铃键 1:防撬键 2:配网键) + 1Byte长按标志
    HI_SYSLINK_MSGID_W2S_KEY_EVENT              = hi_syslink_msg_id_define(HAL_SYSLINK_COMPONENT, HAL_SYSLINK_MODULE, 0x03),

    ///< 1Byte网络连接触发事件 0:连接失败 1:连接成功
    HI_SYSLINK_MSGID_W2S_WIFI_EVENT             = hi_syslink_msg_id_define(HAL_SYSLINK_COMPONENT, HAL_SYSLINK_MODULE, 0x04),


    /* soc to wifi msg id  */
    HI_SYSLINK_MSGID_S2W_GET_VER                = hi_syslink_msg_id_define(HAL_SYSLINK_COMPONENT, HAL_SYSLINK_MODULE, 0x80),

    ///< 1byte设置PIR开关 0:关 1:开
    HI_SYSLINK_MSGID_S2W_SET_PIR                = hi_syslink_msg_id_define(HAL_SYSLINK_COMPONENT, HAL_SYSLINK_MODULE, 0x81),

    HI_SYSLINK_MSGID_S2W_GET_PIR                = hi_syslink_msg_id_define(HAL_SYSLINK_COMPONENT, HAL_SYSLINK_MODULE, 0x82),

    ///< 设置WiFi连接 ssid:32bytes + pwd:64bytes + auth:1byte HI_SYSLINK_WIFI_ATTR
    HI_SYSLINK_MSGID_S2W_SET_WIFI_CONNECT       = hi_syslink_msg_id_define(HAL_SYSLINK_COMPONENT, HAL_SYSLINK_MODULE, 0x83),
    HI_SYSLINK_MSGID_S2W_SET_WIFI_DISCONNECT    = hi_syslink_msg_id_define(HAL_SYSLINK_COMPONENT, HAL_SYSLINK_MODULE, 0x84),

    ///< 获取WiFi状态 ssid:32bytes + pwd:64bytes + auth:1byte + state:1byte(0:未连接,1:连接) HI_SYSLINK_WIFI_STATUS
    HI_SYSLINK_MSGID_S2W_GET_WIFI_STATUS        = hi_syslink_msg_id_define(HAL_SYSLINK_COMPONENT, HAL_SYSLINK_MODULE, 0x85),

    ///< 设置WiFi休眠保活参数 HI_SYSLINK_WIFI_KEEPALIEVE_S
    HI_SYSLINK_MSGID_S2W_SET_WIFI_KEEPALIVE     = hi_syslink_msg_id_define(HAL_SYSLINK_COMPONENT, HAL_SYSLINK_MODULE, 0x86),

    ///< 设置WiFi侧系统时间 HI_SYSLINK_DATE_TIME
    HI_SYSLINK_MSGID_S2W_SET_TIME               = hi_syslink_msg_id_define(HAL_SYSLINK_COMPONENT, HAL_SYSLINK_MODULE, 0x87),
    HI_SYSLINK_MSGID_S2W_GET_TIME               = hi_syslink_msg_id_define(HAL_SYSLINK_COMPONENT, HAL_SYSLINK_MODULE, 0x88),

    ///< 获取电池状态 HI_SYSLINK_BATTERY_STATUS
    HI_SYSLINK_MSGID_S2W_GET_BATTERY_STATUS     = hi_syslink_msg_id_define(HAL_SYSLINK_COMPONENT, HAL_SYSLINK_MODULE, 0x89),

    ///< 1Byte系统模式设置 HI_SYSLINK_SYS_MODE
    HI_SYSLINK_MSGID_S2W_SET_SYSTEM             = hi_syslink_msg_id_define(HAL_SYSLINK_COMPONENT, HAL_SYSLINK_MODULE, 0x90),

    ///< 1Byte灯状态 每bit表示一个灯类型,点亮则对应bit位设置1,否则设置0
    HI_SYSLINK_MSGID_S2W_SET_LED                = hi_syslink_msg_id_define(HAL_SYSLINK_COMPONENT, HAL_SYSLINK_MODULE, 0x91),

    ///< 升级开始 HI_SYSLINK_UPGRADE_START_MSG
    HI_SYSLINK_MSGID_S2W_SET_UPGRADE_START      = hi_syslink_msg_id_define(HAL_SYSLINK_COMPONENT, HAL_SYSLINK_MODULE, 0x92),

    ///< 升级数据发送 HI_SYSLINK_UPGRADE_TRANSMIT_MSG
    HI_SYSLINK_MSGID_S2W_SET_UPGRADE_TRANSMIT   = hi_syslink_msg_id_define(HAL_SYSLINK_COMPONENT, HAL_SYSLINK_MODULE, 0x93),

    ///< 升级完成
    HI_SYSLINK_MSGID_S2W_SET_UPGRADE_FINISH     = hi_syslink_msg_id_define(HAL_SYSLINK_COMPONENT, HAL_SYSLINK_MODULE, 0x94),

    ///< 清除唤醒信息
    HI_SYSLINK_MSGID_S2W_CLEAR_WAKEUP_INFO      = hi_syslink_msg_id_define(HAL_SYSLINK_COMPONENT, HAL_SYSLINK_MODULE, 0x95),

    ///< 接管WiFi侧串口
    HI_SYSLINK_MSGID_S2W_SET_TTY_ENABLE         = hi_syslink_msg_id_define(HAL_SYSLINK_COMPONENT, HAL_SYSLINK_MODULE, 0x96),

} HI_SYSLINK_MSGID_E;

#pragma pack(1) /* 1字节对齐 */

typedef enum {
    HI_SYSLINK_UPGRADE_ACK_OK = 0xA1,
    HI_SYSLINK_UPGRADE_ACK_ERR,
    HI_SYSLINK_UPGRADE_ACK_NONE,
}HI_SYSLINK_UPGRADE_ACK_TYPE;

typedef struct {
    HI_U8 pktType;  /**< 升级包类型 0:uboot 1:kernel */
    HI_U32 pktSize; /**< 升级包大小 */
}HI_SYSLINK_UPGRADE_START_MSG;

typedef struct {
    HI_U32 offset;
    HI_U32 len;
    HI_U8 buf[HAL_SYSLINK_UPGRADE_BLOCK_SIZE];
    HI_U8 bFinished;
}HI_SYSLINK_UPGRADE_TRANSMIT_MSG;

typedef enum {
    HI_SYSLINK_WIFI_SECURITY_OPEN,                  /**< OPEN. CNcomment: 认证类型:开放.CNend */
    HI_SYSLINK_WIFI_SECURITY_WEP,                   /**< WEP. CNcomment: 认证类型:WEP.CNend */
    HI_SYSLINK_WIFISECURITY_WPA2PSK,                /**< WPA-PSK. CNcomment: 认证类型:WPA2-PSK.CNend */
    HI_SYSLINK_WIFI_SECURITY_WPAPSK_WPA2PSK_MIX,    /**< WPA/WPA2-PSK MIX. CNcomment: 认证类型:WPA-PSK/WPA2-PSK混合.CNend */
    HI_SYSLINK_WIFI_SECURITY_WPAPSK,                /**< WPA-PSK. CNcomment: 认证类型:WPA-PSK.CNend */
    HI_SYSLINK_WIFI_SECURITY_WPA,                   /**< WPA. CNcomment: 认证类型:WPA.CNend */
    HI_SYSLINK_WIFI_SECURITY_WPA2,                  /**< WPA2. CNcomment: 认证类型:WPA2.CNend */
    HI_SYSLINK_WIFI_SECURITY_SAE,                   /**< SAE. CNcomment: 认证类型:SAE.CNend */
    HI_SYSLINK_WIFI_SECURITY_UNKNOWN                /**< UNKNOWN. CNcomment: 其他认证类型:UNKNOWN.CNend */
} HI_SYSLINK_WIFI_AUTH_MODE;

typedef enum
{
    HI_SYSLINK_WAKEUP_PIR,          /**< PIR唤醒 */
    HI_SYSLINK_WAKEUP_KEY_BELL,     /**< 门铃按键唤醒 */
    HI_SYSLINK_WAKEUP_KEY_NETCFG,   /**< 配网按键唤醒 */
    HI_SYSLINK_WAKEUP_KEY_PICK,     /**< 防撬按键唤醒 */
    HI_SYSLINK_WAKEUP_WIFI,         /**< WiFi远程唤醒 */
    HI_SYSLINK_WAKEUP_RTC,          /**< RTC定时唤醒 */
    HI_SYSLINK_WAKEUP_POWERON,      /**< 开机上电唤醒 */
    HI_SYSLINK_WAKEUP_LOW_POWER,    /**< 低电唤醒 */
    HI_SYSLINK_WAKEUP_USB_CHARGE,   /**< USB充电唤醒 */
    HI_SYSLINK_WAKEUP_BUFF,
} HI_SYSLINK_WAKEUP_TYPE;

typedef enum
{
    HI_SYSLINK_SYS_MODE_STANDBY = 0,            /**< 待机 */
    HI_SYSLINK_SYS_MODE_LONGALIVE_ENALBE,       /**< 保活 */
    HI_SYSLINK_SYS_MODE_LONGALIVE_DISABLE,      /**< 不保活 */
    HI_SYSLINK_SYS_MODE_REBOOT_SOC,             /**< 重启SOC */
    HI_SYSLINK_SYS_MODE_REBOOT,                 /**< 重启系统 */
    HI_SYSLINK_SYS_MODE_POWEROFF_SOC,           /**< SOC下电 */
    HI_SYSLINK_SYS_MODE_POWERON_SOC,            /**< SOC上电 */
    HI_SYSLINK_SYS_MODE_ULTRA_DEEPSLEEP,        /**< 超深度睡眠      */
} HI_SYSLINK_SYS_MODE;

typedef struct
{
    char ssid[33];
    char pwd[65];
    HI_SYSLINK_WIFI_AUTH_MODE auth;
} HI_SYSLINK_WIFI_ATTR;

typedef struct
{
    char ssid[33];
    HI_SYSLINK_WIFI_AUTH_MODE auth;
    char state;
    int rssi;
} HI_SYSLINK_WIFI_STATUS;

typedef struct {
    HI_U8   bEnable;            /**< 休眠心跳使能 */
    HI_U8   keepAliveData[64];  /**< 休眠心跳包内容 */
    HI_U16  keepAliveLen;       /**< 休眠心跳包内容长度 */
    HI_U8   wakeupData[64];     /**< 唤醒包内容 */
    HI_U16  wakeupLen;          /**< 唤醒包内容长度 */
    struct {
        HI_U32 addr;            /**< 休眠心跳服务器地址 */
        HI_U16 port;            /**< 休眠心跳服务器端口 */
    } server[4];
    HI_U8   serverNum;          /**< 休眠心跳服务器个数 */
    HI_U8   serverThreshold;    /**< 服务器断连触发唤醒阀值个数              */
    HI_U16  interval;           /**< 休眠心跳包间隔(秒) */
}HI_SYSLINK_WIFI_KEEPALIEVE_S;

typedef struct
{
    HI_U8 year; /*2000 as base year,  20 means 2020*/
    HI_U8 month;
    HI_U8 day;
    HI_U8 hour;
    HI_U8 minute;
    HI_U8 second;
} HI_SYSLINK_DATE_TIME;

typedef struct {
    HI_U8  bUsbCharging;        /**< 充电状态 */
    HI_U16 capacity;            /**< 电池电量 单位:mV */
}HI_SYSLINK_BATTERY_STATUS;

typedef struct
{
    HI_SYSLINK_MSGID_E  u32MsgId;                       //事件消息ID号
    HI_U8               data[HAL_SYSLINK_MSG_LEN];
    HI_U16              dataLen;
} HI_SYSLINK_PARM_S;

#pragma pack()


typedef void (*HI_HAL_SYSLINK_CALLBACK_FN_PTR)(HI_SYSLINK_PARM_S stRcvMsg);

HI_U32 HI_HAL_SYSLINK_Init(void);
HI_U32 HI_HAL_SYSLINK_Deinit(void);
HI_U32 HI_HAL_SYSLINK_GetBootInfo(void);
HI_U32 HI_HAL_SYSLINK_GetWakeupType(HI_SYSLINK_WAKEUP_TYPE *penWakeupType);
HI_U32 HI_HAL_SYSLINK_SendTextMsgToWifi(const HI_CHAR *msg, HI_S32 len, HAL_SYSLINK_MsgQos qos);
HI_U32 HI_HAL_SYSLINK_SendMsgToWifiAsync(HI_SYSLINK_PARM_S *pSendMsg);
HI_U32 HI_HAL_SYSLINK_SendMsgToWifiSync(HI_SYSLINK_PARM_S *pSendMsg, HI_SYSLINK_PARM_S *pWaitMsg, HI_U32 timeoutMs);
int HI_HAL_SYSLINK_Register(HI_HAL_SYSLINK_CALLBACK_FN_PTR pfnRecvMsgCbk);
int HI_HAL_SYSLINK_UnRegister(HI_HAL_SYSLINK_CALLBACK_FN_PTR pfnRecvMsgCbk);


#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* __cplusplus */


#endif /* __HI_HAL_SYSLINK_H__ */
